package com.woniu.cms.contract.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.contract.core.entity.Bid;
import com.woniu.cms.contract.core.entity.BidResult;
import com.woniu.cms.contract.core.entity.BidResultManagement;
import com.woniu.cms.contract.core.entity.BidResultVo;
import com.woniu.cms.contract.core.service.IBidResultManagementService;
import com.woniu.cms.contract.core.service.IBidResultService;
import com.woniu.cms.contract.core.service.IBidService;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@RestController
@RequestMapping("/bid-result")
@Api(tags = "用户添加投标单备注")
public class BidResultController {


    @Autowired
    private IBidResultManagementService bidResultManagementService;

    @Autowired
    private IBidResultService bidResultService;


    @ApiOperation("审核结果")
    @GetMapping
    public Result queryBidManagement(String userName, String bidCode, Integer currentPage,Integer pageSize){

        Page<BidResultManagement> page = new Page<>(currentPage,pageSize);

        QueryWrapper<BidResultManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("managers",userName);

        if (bidCode != null && bidCode != ""){
            wrapper.like("bid_code",bidCode);
        }

        Page<BidResultManagement> managementPage = bidResultManagementService.page(page, wrapper);
        List<BidResultManagement> records = managementPage.getRecords();
        long total = managementPage.getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);

        return Result.common(map);
    }

    @ApiOperation("选择投标单展示给编辑界面")
    @GetMapping("{bidCode}")
    public Result queryBidResult(@PathVariable String bidCode){

        QueryWrapper<BidResultManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("bid_code",bidCode);
        BidResultManagement management = bidResultManagementService.getOne(wrapper);

        HashMap<String, Object> map = new HashMap<>();

        map.put("bidMangaementService",management);

        return Result.common(map);
    }

    @ApiOperation("添加数据")
    @PostMapping
    public Result updateBidResult(@RequestBody BidResult bidResult){

        boolean b = bidResultService.save(bidResult);

        return b == true ? Result.ok() : Result.error();
    }

}

